<template>
  <div class="dialog-form">
    <el-form ref="platUrlBaseConfigRef" :model="form" :rules="rules" label-width="100px">
      <el-row :gutter="36">
        <el-col :span="12">
          <el-form-item label="请求地址" prop="url">
            <el-input v-model="form.url" placeholder="请输入请求地址" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="请求方式" prop="reqMethod">
            <el-radio-group v-model="form.reqMethod">
              <el-radio
                v-for="dict in req_method"
                :key="dict.value"
                :value="dict.value"
              >{{dict.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="请求签名开关" prop="signSwitch">
            <el-radio-group v-model="form.signSwitch">
              <el-radio
                v-for="dict in sign_verify_switch"
                :key="dict.value"
                :value="dict.value"
              >{{dict.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="响应验签开关" prop="verifySwitch">
            <el-radio-group v-model="form.verifySwitch">
              <el-radio
                v-for="dict in sign_verify_switch"
                :key="dict.value"
                :value="dict.value"
              >{{dict.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="contentType" prop="contentType">
            <el-select v-model="form.contentType" placeholder="请选择contentType">
              <el-option
                v-for="dict in content_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="渠道号" prop="channelCode">
            <el-input v-model="form.channelCode" placeholder="请输入渠道号" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="是否启用" prop="enabled">
        <el-radio-group v-model="form.enabled">
          <el-radio
            v-for="dict in sys_normal_disable"
            :key="dict.value"
            :value="parseInt(dict.value)"
          >{{dict.label}}</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div class="dialog-footer">
      <el-button @click="onCancel">取消</el-button>
      <el-button type="primary" @click="onConfirm">确认</el-button>
    </div>
  </div>
</template>

<script setup>
import { getPlatUrlBaseConfig, addPlatUrlBaseConfig, updatePlatUrlBaseConfig } from "@/api/bus/platUrlBaseConfig"

const { proxy } = getCurrentInstance()
const { content_type, req_method, sys_normal_disable, sign_verify_switch } = proxy.useDict('content_type', 'req_method', 'sys_normal_disable', 'sign_verify_switch')

const emits = defineEmits(['cancel', 'confirm'])

const platUrlBaseConfigRef = ref()
const form = ref({
  id: null,
  url: null,
  reqMethod: 'GET',
  signSwitch: 'on',
  verifySwitch: 'on',
  contentType: null,
  channelCode: null,
  enabled: 0
})
const rules = {
  url: [{ required: true, message: "请求地址不能为空", trigger: "blur" }],
  channelCode: [{ required: true, message: "渠道号不能为空", trigger: "blur" }],
  reqMethod: [{ required: true, message: "请选择请求方式", trigger: "blur" }],
  contentType: [{ required: true, message: "请选择contentType", trigger: "blur" }],
}

const pageInit = (id) => {
  if (id) {
    getPlatUrlBaseConfig(id).then(response => {
      form.value = response.data
    })
  }
}

const onCancel = () => {
  emits('cancel')
}

const onConfirm = () => {
  platUrlBaseConfigRef.value.validate(valid => {
    if (valid) {
      if (form.value.id) {
        updatePlatUrlBaseConfig(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          platUrlBaseConfigRef.value.resetFields()
          emits('confirm')
        })
      } else {
        addPlatUrlBaseConfig(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          platUrlBaseConfigRef.value.resetFields()
          emits('confirm')
        })
      }
    }
  })
}

defineExpose({
  pageInit
})

</script>

<style lang="scss" scoped>

</style>